class Solution {
public:
    int dp[100005][2];
    int maxSubarraySumCircular(vector<int>& nums) {
        int num = 0, flag = 1, temp = -1000000;
        for (auto& x : nums)
        {
            num += x;
            if (x > 0) flag = 0;
            else temp = max(temp, x);
        }
        if (flag) return temp;
        dp[0][1] = dp[0][0] = nums[0];
        int ans = dp[0][0], _ans = dp[0][1];
        for (int i = 1; i < nums.size(); i++)
        {
            dp[i][1] = max(0, dp[i - 1][1]) + nums[i];
            dp[i][0] = min(0, dp[i - 1][0]) + nums[i];
            ans = min(ans, dp[i][0]);
            _ans = max(_ans, dp[i][1]);
        }
        return max(_ans, num - ans);
    }
};